package aimis.controller;

import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import aimis.constant.ResponseEnum;
import aimis.domain.Authority;
import aimis.domain.Role;
import aimis.dto.PageQuery;
import aimis.dto.Result;
import aimis.dto.TableList;
import aimis.dto.form.AuthorityForm;
import aimis.repo.AuthorityRepo;
import aimis.repo.RoleRepo;

import java.util.List;
import java.util.Set;

@RestController
public class AuthorityController {
    @Autowired
    private AuthorityRepo authorityRepo;
    @Autowired
    private RoleRepo roleRepo;

    @PostMapping("/authority/add")
    public Result add(@RequestBody @Valid AuthorityForm authorityForm) {
        //Form对象-》Domain对象
        Authority authority = new Authority();
//        authority.setSort(authorityForm.getSort());
//        authority.setName(authorityForm.getName());
        BeanUtils.copyProperties(authorityForm, authority);
        authorityRepo.save(authority);
        return Result.success(ResponseEnum.OK);
    }

    @PostMapping("/authority/edit")
    public Result edit(@RequestBody @Valid AuthorityForm authorityForm) {
        //Form对象-》Domain对象
        var findOne = authorityRepo.findById(authorityForm.getId());
        var authority = findOne.get();
        BeanUtils.copyProperties(authorityForm, authority);
        authorityRepo.save(authority);
        return Result.success(ResponseEnum.OK);
    }

    @PostMapping("/authority/get")
    public Result get(Long id) {
        var authority = authorityRepo.findById(id);
        if (authority.isPresent()) {
            return Result.success(ResponseEnum.OK).add("authority", authority.get());
        } else {
            return Result.error(ResponseEnum.DATA_NOT_EXIST);
        }
    }

    @PostMapping("/authority/delete")
    public Result delete(@RequestBody @Validated(AuthorityForm.DeleteAuthority.class) AuthorityForm authorityForm) {
        //Form对象-》Domain对象
        var findOne = authorityRepo.findById(authorityForm.getId());
        List<Role> byAuthoritiesId = roleRepo.findByAuthoritiesId(authorityForm.getId());
        for (var r : byAuthoritiesId) {
            r.getAuthorities().remove(findOne.get());
        }
        roleRepo.saveAll(byAuthoritiesId);
        authorityRepo.deleteById(authorityForm.getId());
        return Result.success(ResponseEnum.OK);
    }

    @PostMapping(value = "/authority/findByQuery")
    public TableList<Authority> findByQuery(String name, PageQuery pageQuery) {
        var pageable = pageQuery.toPageable();
        if (name != null && name.isBlank())
            name = null;
        var authorities = authorityRepo.findByQuery(name, pageable);
        return TableList.create(authorities);
    }

    @PostMapping(value = "/authority/findAll")
    public List<Authority> findAll() {
        return authorityRepo.findAll();
    }

    @PostMapping(value = "/authority/findByNameLike")
    public Set<Authority> findByNameLike(String name) {
        if (name != null && name.isBlank())
            name = null;
        return authorityRepo.findByNameLike(name);
    }
}
